<template>
  <el-scrollbar class="navigation-sidebar" height="100%">
    <el-menu
      :default-active="defaultActive"
      class="sidebar-menu"
      :unique-opened="true"
      @select="handleMenuSelect"
    >
      <el-sub-menu
        v-for="category in menuList"
        :key="category.oneMenuId"
        :index="String(category.oneMenuId)"
        class="menu-category"
      >
        <template #title>
          <div class="menu-item-primary">
            <div class="menu-icon">
              <svg class="icon-svg" aria-hidden="true">
                <use :xlink:href="category.icon"></use>
              </svg>
            </div>
            <span class="menu-name">{{ category.oneMenuName }}</span>
          </div>
        </template>
        
        <el-menu-item
          v-for="subCategory in category.twoMenuList"
          :key="subCategory.twoMenuId"
          :index="`${category.oneMenuId}-${subCategory.twoMenuId}`"
          class="submenu-item"
        >
          <span class="submenu-name">{{ subCategory.twoMenuName }}</span>
          <el-badge
            :value="subCategory.websiteList.length"
            class="submenu-badge"
            :hidden="subCategory.websiteList.length === 0"
          />
        </el-menu-item>
      </el-sub-menu>
    </el-menu>
  </el-scrollbar>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { MenuWebsiteVO } from '@/interfaces/data'

/**
 * Props 定义
 */
const props = defineProps<{
  /** 菜单网站数据列表 */
  menuList: MenuWebsiteVO[]
  /** 选中的二级菜单ID */
  selectedSubCategoryId: number | null
}>()

/**
 * Emits 定义
 */
const emit = defineEmits<{
  /** 二级菜单被选中时触发 */
  'subcategory-selected': [subCategoryId: number]
}>()

/**
 * 计算默认激活的菜单项（选中的二级菜单）
 * Element Plus 的 el-menu-item 组件需要字符串格式的索引
 * 格式：一级菜单ID-二级菜单ID
 */
const defaultActive = computed(() => {
  if (props.selectedSubCategoryId !== null) {
    // 遍历菜单列表，找到包含该二级菜单的一级菜单
    for (const category of props.menuList) {
      const found = category.twoMenuList.find(sub => sub.twoMenuId === props.selectedSubCategoryId)
      if (found) {
        return `${category.oneMenuId}-${props.selectedSubCategoryId}`
      }
    }
  }
  return ''
})

/**
 * 处理菜单项选择（二级菜单点击）
 * @param index 菜单项索引，格式：一级菜单ID-二级菜单ID
 */
function handleMenuSelect(index: string) {
  // 从 index 中提取一级菜单ID和二级菜单ID字符串
  const parts = index.split('-')
  if (parts.length === 2) {
    const parentIdStr = parts[0]
    const subCategoryIdStr = parts[1]
    
    // 从原始数据中查找对应的二级菜单，直接使用原始ID值，避免字符串转数字的精度丢失
    for (const category of props.menuList) {
      // 先匹配一级菜单ID（字符串比较）
      if (String(category.oneMenuId) === parentIdStr) {
        // 在匹配的一级菜单下查找二级菜单
        const found = category.twoMenuList.find(
          sub => String(sub.twoMenuId) === subCategoryIdStr
        )
        if (found) {
          // 直接使用原始数据中的ID值，不进行任何转换
          emit('subcategory-selected', found.twoMenuId)
          return
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.navigation-sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--theme-card-bg, #ffffff);
  border-right: 1px solid var(--theme-border);
  position: sticky;
  top: 0; /* 相对于滚动容器，顶部对齐 */
  align-self: flex-start;
  height: calc(100vh - 64px); /* 固定高度：视口高度减去头部高度 */
  z-index: 10;
  margin-top: 20px; /* 菜单离上面一点距离 */
  border-radius: 0 12px 12px 0; /* 右侧圆角 */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08); /* 添加阴影以区别于背景 */
  margin-left: 0; /* 靠近左侧，无左边距 */
  
  :deep(.el-scrollbar__wrap) {
    overflow-x: hidden;
  }
  
  :deep(.el-scrollbar__bar) {
    &.is-vertical {
      right: 0;
      width: 6px;
      
      .el-scrollbar__thumb {
        background: rgba(144, 147, 153, 0.3);
        border-radius: 3px;
        transition: background 0.3s ease;
        
        &:hover {
          background: rgba(144, 147, 153, 0.5);
        }
      }
    }
  }
}

.sidebar-menu {
  border-right: none;
  background: transparent;
  
  // 一级菜单（sub-menu）样式
  :deep(.el-sub-menu) {
    .el-sub-menu__title {
      padding: 12px 20px;
      height: auto;
      line-height: normal;
      transition: all 0.3s ease;
      
      &:hover {
        background: rgba(102, 126, 234, 0.08);
        color: var(--theme-text);
      }
    }
    
    &.is-opened > .el-sub-menu__title {
      background: rgba(102, 126, 234, 0.05);
      color: var(--theme-text);
    }
    
    .menu-item-primary {
      display: flex;
      align-items: center;
      width: 100%;
      
      .menu-icon {
        width: 20px;
        height: 20px;
        margin-right: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--theme-primary);
        flex-shrink: 0;

        .icon-svg {
          width: 20px;
          height: 20px;
        }
      }

      .menu-name {
        flex: 1;
        font-size: 15px;
        font-weight: 500;
        color: var(--theme-text);
      }
    }
  }
  
  // 二级菜单（menu-item）样式
  :deep(.el-menu-item) {
    padding: 10px 20px 10px 52px !important;
    height: auto;
    line-height: normal;
    transition: all 0.3s ease;
    
    &:hover {
      background: rgba(102, 126, 234, 0.08);
      color: var(--theme-primary);
    }
    
    &.is-active {
      background: rgba(102, 126, 234, 0.12);
      color: var(--theme-primary);
      font-weight: 600;
      
      &::before {
        display: none; // 移除 Element Plus 默认的激活指示器
      }
    }
    
    .submenu-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      
      .submenu-name {
        font-size: 14px;
        flex: 1;
      }

      .submenu-badge {
        :deep(.el-badge__content) {
          font-size: 11px;
          padding: 0 6px;
          height: 18px;
          line-height: 18px;
          background: var(--theme-primary);
        }
      }
    }
  }
}

/* Element Plus 滚动条样式已在上面定义 */

/**
 * 响应式设计
 * 移动端（宽度小于1024px）时，调整侧边栏样式
 */
@media (max-width: 1024px) {
  .navigation-sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--theme-border);
    max-height: 300px;
    margin-left: 0;
    margin-top: 0;
    border-radius: 0;
    box-shadow: none;
  }
}
</style>

